package com.leyou.user.web;

import com.leyou.auth.utils.UserContext;
import com.leyou.user.dto.AddressDTO;
import com.leyou.user.entity.Address;
import com.leyou.user.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author BaiZY on 2020/9/7.
 * @version 1.0
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 根据id查詢地址信息
     * @param id 地址id
     * @return 地址信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Address> queryAddressById(@PathVariable("id") Long id){
        Address address = addressService.queryAddressById(id);
        return ResponseEntity.ok(address);
    }

    /**
     * 新增地址
     * @param address
     * @return
     */
    @PostMapping("/add")
    public ResponseEntity<Boolean> addAddress(@RequestBody @Valid Address address){
        boolean success = addressService.addAddress(address);
        return ResponseEntity.status(HttpStatus.CREATED).body(success);
    }

    /**
     * 查询所有的地址
     * @return
     */
    @GetMapping("/find")
    public ResponseEntity<List<Address>> findAddress(){
        List<Address> list = addressService.findAddress();
        return ResponseEntity.ok(list);
    }

    /**
     * 删除地址
     */
    @DeleteMapping("/delete")
    public ResponseEntity<Boolean> deleteAddress(@RequestParam("userId")Long userId, @RequestParam("id")Long id){
        boolean flag = addressService.deleteAddress(userId,id);
        return ResponseEntity.ok(flag);
    }

    /**
     * 修改默認地址
     * @param userId
     * @param id
     * @return
     */
    @PutMapping("/updateDefault")
    public ResponseEntity<Boolean> updateDefault(@RequestParam("userId")Long userId, @RequestParam("id")Long id){
        boolean flag = addressService.updateDefault(userId,id);
        return ResponseEntity.ok(flag);
    }

    @PutMapping("/updateAddress")
    public ResponseEntity<Boolean> updateAddress(@RequestBody Address address){
        boolean flag = addressService.updateAddress(address);
        return ResponseEntity.ok(flag);
    }

    @GetMapping("/findAddressByuserId")
    public ResponseEntity<List<Address>> findAddressByuserId(){
        List<Address> list  = addressService.findAddressByuserId();
        return ResponseEntity.ok(list);
    }
}
